//This servlet handles deletion of the friend from the buddylist for the corresponding  nym.This servlet is called by the 
import java.sql.*;
import java.util.Date;
import java.io.*;
import java.util.*;
import java.util.HashMap;
import javax.servlet.http.*;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

public class DelFriendServlet extends HttpServlet{

	//Initialize the servlet	
	public void init(ServletConfig config)
	{
	  System.out.println("FriendsServlet: init()");
	}
	
	//This is the service method that gets the parameters submitted on the html page and then calls the DeleteFriends function to delete the friends from the list.
	public void doPost(HttpServletRequest request,HttpServletResponse response)    
		throws ServletException, IOException 
	{
		int ibuddyno;
		String strPrevNym=null;
		// set the response type
		response.setContentType("text/xml; charset=UTF-8"); 
		response.setHeader("Cache-Control","no-cache");
		//Get the parametes
		String strNym=request.getParameter("nym");
		//String strNym="Home";
		String strFriends=request.getParameter("friends");
		//String strFriends="Alex_work,Alex";
		int success=this.DeleteFriends(strNym,strFriends);
		// obtain the writer to write the response
		PrintWriter out = response.getWriter();
		//send the xml response.The success parameter indicates if the deletion of friend from the corresponding nym was successful.
		out.println("<?xml version=\"1.0\"?>");  
		out.println("<Success>");
		out.println("<valid>");
		out.println(success);
		out.println("</valid>");
		out.println("</Success>");
	}
		
		
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// Connect()
	// Connects to the database
	// return value of type Connection
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static Connection Connect()
	{
		Connection conn = null;
		
		try
		{
			//Defines the Query statements
			//ResultSet rsBuddy,rsInternal,rsNymID;
			//Connect to the database
			String userName = "nettrust";
			String password = "nettrust06162006";
			String url= "jdbc:mysql://localhost/NetTrust";
			Class.forName ("com.mysql.jdbc.Driver").newInstance ();
			conn = DriverManager.getConnection (url, userName, password);
			System.out.println ("Database connection established");
		}
		catch( Exception e )
		{
			// Do nothing
			System.out.println("Connection failed");
		};  
		return( conn );
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function Connect()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// DeleteFriends()
	// Deletes the friends from the buddy list for that particular nym
	// return success as 1 if the deletion was successful else it returns a 0
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	public static int DeleteFriends(String strNymName,String strFriends)
	{
		ResultSet rsFriends;
		Connection conn=Connect();
		String strBuddyID=null;
		String strNymID=GetNymID(strNymName);
		StringTokenizer strTokBuddyName=new StringTokenizer(strFriends,",");
		try
		{
			Statement stDelBuddyName=conn.createStatement();
			Statement stBuddyID=conn.createStatement();
			while(strTokBuddyName.hasMoreTokens())
			{
				rsFriends=stBuddyID.executeQuery("select NymID from nymnamemap where nymnamemap.NymName= '"+strTokBuddyName.nextToken()+"'"); 
				while(rsFriends.next())
				{
					strBuddyID=rsFriends.getString("NymID");
				}
				//Get the names of the broadcasters.
				stDelBuddyName.executeUpdate("delete from nyminfo where nyminfo.NymID='"+strNymID+"' and BuddyID='"+strBuddyID+"'"); 
			}
			return 1;
		}
		catch(Exception e)
		{
			System.out.println(" The following Exception occured"+e);
			return 0;
		}
	}
	/* * * * * * * * * * * * * * * * * * * * * * * * * * *h * * */
	// end of function DeleteFriends()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */	
	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// GetNymID()
	// Gets the 128 bit NymID corresponding to a nym using the nym name as the parameter
	// returns 128 bit NymID
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	public static String GetNymID(String strNym)
	{
		Connection conn =Connect();
		String strNymid=null;
		ResultSet rsNymID;
		try
		{
			Statement getNymID= conn.createStatement();
		      //Get the nymid corresponding to the nym name from the database 
			rsNymID=getNymID.executeQuery("select nymid from nymnamemap where nymname='" + strNym + "'");
			while( rsNymID.next() )
			{
				strNymid=rsNymID.getString("nymid");
			}
		}
		catch( Exception e )
		{
			System.out.println ( "Database Exception is " + e );
		};  
		return(strNymid);
	}	
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	// end of function GetNymID()
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */		
	
		
	
	
	public void destroy() {
		System.out.println("BroadCasterServlet: destroy()");
	}
}